package com.itheima.imclient100.adapter;

import android.graphics.drawable.AnimationDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.chat.EMTextMessageBody;
import com.hyphenate.util.DateUtils;
import com.itheima.imclient100.R;

import java.util.Date;
import java.util.List;

/**
 * Created by fullcircle on 2017/7/4.
 */

public class ChatAdatper extends RecyclerView.Adapter<ChatAdatper.MyViewHolder1> {
    public void setMessageList(List<EMMessage> messageList) {
        this.messageList = messageList;
    }

    public List<EMMessage> getMessageList() {
        return messageList;
    }

    private List<EMMessage> messageList;

    public ChatAdatper(List<EMMessage> messageList) {
        this.messageList = messageList;
    }

    @Override
    public MyViewHolder1 onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if(viewType == 1){
            //说明是发送的消息
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_chat_item_send, parent, false);
        }else{
            //说明是收到的消息
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_chat_item, parent, false);

        }
        return new MyViewHolder1(view);
    }

    @Override
    public void onBindViewHolder(ChatAdatper.MyViewHolder1 holder, int position) {
        //①消息内容的展示
        EMMessage message = messageList.get(position);
        //先获取EMMessageBody 强转成EMTextMessageBody
        EMTextMessageBody messageBody = (EMTextMessageBody) message.getBody();
        //从EMTextMessageBody 中 获取消息内容
        holder.tv_content.setText(messageBody.getMessage());
        //②消息时间是否要展示
        long msgTime = message.getMsgTime();
        if(position == 0){
            //如果是第一条 需要跟当前时间进行比较 如果跟当前时间相距较远 就显示这个时间
            if(DateUtils.isCloseEnough(msgTime,System.currentTimeMillis())){
                //说明这条消息刚刚收到 不需要展示时间
                holder.tv_time.setVisibility(View.GONE);
            }else{
                //说明这条消息发送的时间比较久 需要展示时间
                holder.tv_time.setText(DateUtils.getTimestampString(new Date(msgTime)));
            }
        }else{
            //如果不是第一条 就与上一条消息进行比较
            if(DateUtils.isCloseEnough(msgTime,messageList.get(position-1).getMsgTime())){
                //说明两条消息时间间隔比较短 不需要展示时间
                holder.tv_time.setVisibility(View.GONE);
            }else{
                //说明两条消息时间间隔比较久 需要展示时间
                holder.tv_time.setText(DateUtils.getTimestampString(new Date(msgTime)));
            }
        }
        //③如果是发送的消息 需要控制消息的发送状态的展示
        if(message.direct()== EMMessage.Direct.SEND){
            switch (message.status()){
                case SUCCESS:
                    holder.iv_status.setVisibility(View.GONE);
                    break;
                case FAIL:
                    holder.iv_status.setVisibility(View.VISIBLE);
                    holder.iv_status.setImageResource(R.mipmap.msg_error);
                    break;
                case INPROGRESS:
                    holder.iv_status.setVisibility(View.VISIBLE);
                    holder.iv_status.setImageResource(R.drawable.send_animation);
                    AnimationDrawable drawable = (AnimationDrawable) holder.iv_status.getDrawable();
                    drawable.start();
                    break;
            }
        }

    }

    @Override
    public int getItemViewType(int position) {
        EMMessage message = messageList.get(position);
        EMMessage.Direct direct = message.direct();

        return direct== EMMessage.Direct.RECEIVE?0:1;
    }

    @Override
    public int getItemCount() {
        return messageList == null ? 0:messageList.size();
    }

    class MyViewHolder1 extends RecyclerView.ViewHolder{

        TextView tv_time;
        TextView tv_content;
        ImageView iv_status;
        public MyViewHolder1(View itemView) {
            super(itemView);
            tv_time = (TextView) itemView.findViewById(R.id.tv_time);
            tv_content = (TextView) itemView.findViewById(R.id.tv_message);
            iv_status = (ImageView) itemView.findViewById(R.id.iv_state);
        }
    }
}